package com.example.diabeco;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;

public class ConfigurarAlarmaActivity extends Activity {
	private static final int OK_RESULT_CODE = 0;
	private static final int CANCEL_CODE = 4;
	String dias = null;
	String hora = null;
	Intent intent = new Intent();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_configurar_alarma);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.configurar_alarma, menu);
		return true;
	}

	
	/** Se llama al hacer click en "Guardar" */
	public void sendMsgConfigAlarmaBtnGuardar(View view) {
		
		TimePicker tPicker;
		ToggleButton tbLunes, tbMartes, tbMiercoles, tbJueves, tbViernes, tbSabado, tbDomingo;
		
		
		tPicker = (TimePicker) findViewById(R.id.timePicker1);
		tbLunes = (ToggleButton) findViewById(R.id.toggleButton1);
		tbMartes = (ToggleButton) findViewById(R.id.toggleButton2);
		tbMiercoles = (ToggleButton) findViewById(R.id.toggleButton3);
		tbJueves = (ToggleButton) findViewById(R.id.toggleButton4);
		tbViernes = (ToggleButton) findViewById(R.id.toggleButton5);
		tbSabado = (ToggleButton) findViewById(R.id.toggleButton6);
		tbDomingo = (ToggleButton) findViewById(R.id.toggleButton7);
		

		if (tbLunes.isChecked()) {
 			dias = "Lu";
        } 

		if (tbMartes.isChecked()) {
            if (dias.isEmpty()) {
            	dias = "Ma";
            }
            else {	
            	dias = dias.concat("-Ma");
			}
        } 
		
		if (tbMiercoles.isChecked()) {
            if (dias.isEmpty()) {
            	dias = "Mi";
            }
            else {	
            	dias = dias.concat("-Mi");
			}
        } 

		if (tbJueves.isChecked()) {
            if (dias.isEmpty()) {
            	dias = "Ju";
            }
            else {	
            	dias = dias.concat("-Ju");
			}
        } 
		
		if (tbViernes.isChecked()) {
            if (dias.isEmpty()) {
            	dias = "Vi";
            }
            else {	
            	dias = dias.concat("-Vi");
			}
        } 
		
		if (tbSabado.isChecked()) {
            if (dias.isEmpty()) {
            	dias = "Sa";
            }
            else {	
            	dias = dias.concat("-Sa");
			}
        } 
		
		if (tbDomingo.isChecked()) {
            if (dias.isEmpty()) {
            	dias = "Do";
            }
            else {	
            	dias = dias.concat("-Do");
			}
        } 
		
		//Pasamos la hora a un String
		hora = tPicker.getCurrentHour().toString();
		hora = hora.concat(":");
		hora = hora.concat(tPicker.getCurrentMinute().toString());
		
		//Abrimos base de datos
		BaseDeDatos dbAlarmas = new BaseDeDatos(this, BaseDeDatos.DATABASE_NAME, null, 1);
		SQLiteDatabase db = dbAlarmas.getWritableDatabase();
		
		ContentValues nueva_alarma = new ContentValues();
		nueva_alarma.put("hora", hora.toString());
		nueva_alarma.put("dias", dias.toString());
		
		db.insert("alarmas", null, nueva_alarma);
				
		//Cerramos Base de Datos
		dbAlarmas.close();	
		
//		//Cargamos el intent a devolver
//        intent.putExtra("Hora", hora.toString());
//		intent.putExtra("Dias", dias.toString());
//		setResult(OK_RESULT_CODE, intent);
		finish();
	}
	
	/** Se llama al hacer click en "Cancelar" */
	public void sendMsgConfigAlarmaBtnCancelar(View view) {
//		setResult(CANCEL_CODE, intent);
		finish();
	}	

}
